package org.example.controller;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import org.example.domain.City;
import org.example.domain.SysUser;
import org.example.mapper.CityMapper;
import org.example.service.CityService;
import org.example.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("city")
public class CityController {
    @Autowired
    CityService cityService;
    @Autowired
    CityMapper cityMapper;
    @RequestMapping("cityMenu")
    public Result cityMenu(){
        List<City> nodeList = cityService.listCityMenu();
        //配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        //转换器 (含义:找出父节点为字符串零的所有子节点, 并递归查找对应的子节点, 深度最多为 3)
        List<Tree<String>> treeNodes = TreeUtil.<City, String>build(nodeList, "1", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getId()+"");
                    tree.setName(treeNode.getCityname());
                    tree.setParentId(treeNode.getPid()+"");
                });
        return Result.success(treeNodes);
    }

    @RequestMapping("cityMenuJoin")
    public Result cityMenuJoin(){
        List<City> list = cityMapper.cityMenuJoin();
        return Result.success(list);
    }
}
